package com.tomatozq.controller;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.tomatozq.dto.DocumentInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Slf4j
@Controller
@RequestMapping("/document")
public class DocumentController {
    @Value("${document.queue.name}")
    private String queueName;

    @Autowired
    private AmqpTemplate amqpTemplate;

    @RequestMapping("add")
    @ResponseBody
    public String add(String srcFilePath){
        DocumentInfo documentInfo = new DocumentInfo();

        documentInfo.setSrcFilePath("G:\\watermark\\PowerDesigner教程(完整).docx");

        ObjectMapper mapper = new ObjectMapper();

        try {
            log.info("发送消息到队列:" + mapper.writeValueAsString(documentInfo));
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }

        amqpTemplate.convertAndSend(queueName,documentInfo);

        return "ok";
    }
}
